<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Controller;
class Fos extends Common
{
    public function _initialize(){
        parent::_initialize();
		//判断是否是财务
		$group_id = db('admin')->WHERE('admin_id', session('aid'))->value("group_id");
		$this->assign('group_id', $group_id);
    }

	public function index(){
		return $this->fetch("shop");
	}
	
	public function buy_list(){
		if(request()->isPost()) {
            $key = input('post.key');
            $this->assign('testkey', $key);
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list = db('buy_list')
                ->where('m_id', 'like', "%" . $key . "%")
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
		    
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['shop'] = db('shop')->where('id', $v['shop_id'])->value("name");
                $list['data'][$k]['name'] = db('users')->where('id', $v['m_id'])->value("username");
                $list['data'][$k]['money'] = $v['count']*2000;
				$level = db('users')->where('id', $v['m_id'])->value('level');
				$amount = db('rule')->where('level', "F".$level)->value('amount');
                $list['data'][$k]['limit'] = $amount;
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
		
		return $this->fetch();
	}
	
	
	public function first_pass(){
		if(request()->isPost()) {
		    $id = input('param.id');
		    $status = input('param.status');
			$ret = db('buy_list')->where('id', $id)->update(['status'=>$status, 'first_pass_time'=>date("Y-m-d H:i:s")]);
			if($ret){
				echo 1;
			}else{
				echo 0;
			}
		}
	}
	
	
	public function twice_pass(){
		if(request()->isPost()) {
		    $id = input('param.id');
		    $status = input('param.status');
		    $row = db('buy_list')->where('id', $id)->find();
			$m_id = $row['m_id'];
			$ret = db('buy_list')->where('id', $id)->update(['status'=>$status, 'twice_pass_time'=>date("Y-m-d H:i:s")]);
			if($ret){
				if($status == 2){
				    db('users')->where('id', $m_id)->setInc('freeze_fos', $row['count']);
				}
				echo 1;
			}else{
				echo 0;
			}
		}
	}
	
    //商家列表
    public function shop(){
        if(request()->isPost()) {
            $key = input('post.key');
            $this->assign('testkey', $key);
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list = db('shop')
                ->where('name', 'like', "%" . $key . "%")
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
		    
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:s',$v['addtime']);
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
    }
	
    public function add(){
        if(request()->isPost()) {
            //构建数组
            $data = input('post.');
            $data['addtime'] = time();
           
            db('shop')->insert($data);
            $result['code'] = 1;
            $result['msg'] = '商家添加成功!';
            cache('adList', NULL);
            $result['url'] = url('index');
            return $result;
        }else{
            $this->assign('info','null');
            return $this->fetch('form');
        }
    }
    public function edit(){
        if(request()->isPost()) {
            $data = input('post.');
            
            db('shop')->update($data);
            $result['code'] = 1;
            $result['msg'] = '商家修改成功!';
            cache('adList', NULL);
            $result['url'] = url('index');
            return $result;
        }else{
            $adtypeList=db('ad_type')->order('sort')->select();
            $id=input('id');
            $adInfo=db('shop')->where(array('id'=>$id))->find();
            $this->assign('adtypeList',json_encode($adtypeList,true));
            $this->assign('info',json_encode($adInfo,true));
            $this->assign('title',lang('edit').lang('ad'));
            return $this->fetch('form');
        }
    }
    public function editState(){
        $id=input('post.id');
        $open=db('shop')->where(array('id'=>$id))->value('open');//判断当前状态情况
        if($open==1){
            $data['open'] = 0;
            db('shop')->where(array('id'=>$id))->setField($data);
            $data['status'] = 1;
        }else{
            $data['open'] = 1;
            db('shop')->where(array('id'=>$id))->setField($data);
            $data['status'] = 1;
        }
        cache('adList', NULL);
        return $data;
    }
    public function adOrder(){
        $ad=db('shop');
        $data = input('post.');
        if($ad->update($data)!==false){
            cache('adList', NULL);
            return $result = ['msg' => '操作成功！','url'=>url('index'), 'code' =>1];
        }else{
            return $result = ['code'=>0,'msg'=>'操作失败！'];
        }
    }
    public function del(){
        db('shop')->where(array('id'=>input('id')))->delete();
        cache('adList', NULL);
        return ['code'=>1,'msg'=>'删除成功！'];
    }
    public function delall(){
        $map['id'] =array('in',input('param.ids/a'));
        db('shop')->where($map)->delete();
        cache('adList', NULL);
        $result['msg'] = '删除成功！';
        $result['code'] = 1;
        $result['url'] = url('index');
        return $result;
    }

    /***************************位置*****************************/
    //位置
    public function type(){
        if(request()->isPost()) {
            $key = input('key');
            $this->assign('testkey', $key);
            $list = db('ad_type')->where('name', 'like', "%" . $key . "%")->order('sort')->select();
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
        }
        return $this->fetch();
    }
    public function typeOrder(){
        $ad_type=db('ad_type');
        $data = input('post.');
        if($ad_type->update($data)!==false){
            return $result = ['msg' => '操作成功！','url'=>url('type'), 'code' =>1];
        }else{
            return $result = ['code'=>0,'msg'=>'操作失败！'];
        }
    }
    public function addType(){
        if(request()->isPost()) {
            db('ad_type')->insert(input('post.'));
            $result['code'] = 1;
            $result['msg'] = '商家位保存成功!';
            $result['url'] = url('type');
            return $result;
        }else{
            $this->assign('title',lang('add').lang('ad').'位');
            $this->assign('info','null');
            return $this->fetch('typeForm');
        }
    }
    public function editType(){
        if(request()->isPost()) {
            db('ad_type')->update(input('post.'));
            $result['code'] = 1;
            $result['msg'] = '商家位修改成功!';
            $result['url'] = url('type');
            return $result;
        }else{
            $type_id=input('param.type_id');
            $info=db('ad_type')->where('type_id',$type_id)->find();
            $this->assign('title',lang('edit').lang('ad').'位');
            $this->assign('info',json_encode($info,true));
            return $this->fetch('typeForm');
        }
    }
    public function delType(){
        $map['type_id'] = input('param.type_id');
        db('ad_type')->where($map)->delete();//删除商家位
        db('shop')->where($map)->delete();//删除该商家位所有商家
        return ['code'=>1,'msg'=>'删除成功！'];
    }
}